<?php

namespace App\Model;

use Illuminate\Auth\Authenticatable;
use Illuminate\Foundation\Auth\User;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use Illuminate\Notifications\Notifiable;

class Users extends User implements JWTSubject
{
    use Authenticatable;
	use Notifiable;
	use EntrustUserTrait;


	protected $table='users';
//	protected $fillable=['name','email','password',
//	'realname','phone','status','company_id'
//	];
	protected $guarded=[];
	protected $hidden=[ 'password', 'remember_token'];
	public function getJWTIdentifier()
	{
		return $this->getKey(); // Eloquent model method
	}
	public function getJWTCustomClaims()
	{
		return [];
	}
	public function allrole($uid){
		return $this->where('id',$uid)->with('role_user')->pluck('role_id');
	}

//	获取所有的员工列表
	public function lists($where){
		$res= $this->where(function ($query) use($where){
			$query->where(['company_id'=>$where['company_id']]);
			if($where['name'] !=null ){
				$query->where('name','like','%'.$where['name'].'%');
			}
			if($where['start_time'] !=null ){
				$query->where('created_at','>',$where['start_time']);
			}
			if($where['end_time'] !=null ){
				$query->where('created_at','<',$where['end_time']);
			}
			if($where['phone'] !=null ){
				$query->where('phone','like','%'.$where['phone'].'%');
			}
			if($where['status'] !=null ){
				$query->where(['job'=>$where['status']]);
			}
			if($where['status_'] !=null ){
				$query->where(['status'=>$where['status_']]);
			}
			if($where['department'] !=null ){
				$query->where(['department'=>$where['department']]);
			}
		})->with('task_status')->with('departments')->paginate(config('app.page'));
		 foreach($res as $kk=>$vv){
			 $res[$kk]['name']=$vv['realname'];
		 }
		return $res;
	}

	public function alluser($where){
		return $this->where(function ($query) use($where){
			$query->where(['company_id'=>$where['company_id']]);
			if($where['status'] !=null){
				$query->where(['status'=>$where['status']]);
			}
			if($where['name'] !=null){
				$query->where('name','like','%'.$where['name'].'%');
			}
		})->with('department')->with(['staff'=>function($query){
			$query->where(['type'=>1]);
		}])->get();

	}

	public function staff(){
		return $this->hasMany('App\Model\Staff','uid');
	}
	public function staffs(){
		return $this->hasMany('App\Model\Staff','uid')->with('staff_task');
	}

	public function department(){
		return $this->belongsTo('App\Model\Department','department');
	}
	public function usersInfo($uid){
		return $this->where(['id'=>$uid])->first()->realname;
	}

	public function createUser($data){
		return $this->create($data)->id;
	}

	public function roles()
	{
		return $this->belongsToMany(Role::class, 'role_user', 'user_id', 'role_id', 'id', 'id');
	}


//	获取所有的任务
	public function alltask($uid){
		$data=$this->where(['id'=>$uid])->with(['staffs'=>function($query){
			$query->select('id','uid');
		}])->first();
		return $data;
	}


	//查询该用户接取的所有任务
	public function user_all_task($where){
		 $data=$this->where(['id'=>$where['uid']])
			->with(['staffs'=>function($query) use($where){
				$arr=$query->with(['staff_task'=>function($query){
					$query->with('task');
				}]);
				if(isset($where['start_time']) && isset($where['end_time'])){
					$arr->where(function ($query) use($where){
						$query->whereBetween('created_at',[$where['start_time'],$where['end_time']]);
					});
				}

			}])

			->first();
		return $data;
	}
	//查询该用户接取的所有任务
//	public function user_all_task($where){
//		$data=$this->where(['id'=>$where['uid']])
//			->with(['staffs'=>function($query) use($where){
//				$query->with(['staff_task'=>function($query){
//					$query->with('task');
//				}])->whereBetween('created_at',[$where['start_time'],$where['end_time']]);
//			}])
//			->where(function ($query) use($where){
//				$query->whereBetween('created_at',[$where['start_time'],$where['end_time']]);
//			})
//			->first();
//		return $data;
//	}


//	获取用户所有的中止任务
	public function user_staff_leave($uid){
		return $this->where(['id'=>$uid])->with(['staff'=>function($query){
			$query->with('staff_leave')->with('store_station');
		}])->first();
	}

//	查询所属部门
	public function departments(){
		return $this->belongsTo('App\Model\Department','department');
	}

//	查询任务状态
	public function task_status(){
		return $this->belongsTo('App\Model\Task_status','task_status');
	}

	public function user_role($uid){
		return $this->where(['id'=>$uid])->with('roles')->get();
	}

//	添加成员
	public function department_staffs($department_name,$group_id){
		if($group_id !=null){
			$user_idData=Group_member::where(['group_id'=>$group_id])->pluck('user_id');
		}
		$data=Department::where('name','=',$department_name)->first();
		if($data){
			$department_id=$data->id;
		}else{
			return null;
		}
		$data=$this->where(['department'=>$department_id])
			->with('departments')
//			->with(['userinfo' => function ($query) {
//				$query->select(['id','name','realname']);
//				$query->with(['roles' => function ($query) {
//					$query->select(['display_name']);
//				}]);
//			}])
				->select(['id','name','realname','department','is_manage','address'])
				->with(['roles'=>function($query){
				$query->select(['display_name']);
			}])
			->get();

		return $data;
		$array=[];
		foreach($data as $key=>$value){
			if(count($value['userinfo']['roles'])){
				$array[$key]=$value;
			}
		}
		return $array;
	}

    /**
     * 通知反向多对多
     *
     * Date: 2018/7/11
     * @author Leon
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
	public function circular(){
        return $this->belongsToMany(Circular::class,'user_circular_pivot','user_id','circular_id')->withPivot('read');
    }


}



